<?php

class IndexController extends Zend_Controller_Action
{
	private $_auth;
	private $_acl;
    public function init(){
        $acl                = new My_Acl();
        $auth               = $acl->hasIdentity($this->_request);
        $this->_auth        = $auth->getIdentity();
        $this->view->auth   = $this->_auth;
        $this->_acl         = $acl;
        $this->view->acl    = $this->_acl;
    }
    public function updateAction(){
        $modelGame = new Default_Model_Game();
        $modelGameChannel = new Admin_Model_GameChannel();
        $modelGameServer = new Admin_Model_GameServer();
        $modelGameChannelServer = new Admin_Model_GameChannelServer();
        $modelGameCluster = new Admin_Model_GameCluster();
        
        $gameList = $modelGame->getAllData();
        foreach($gameList as $key => $game){
            if(!file_exists(CONFIG_PATH."/{$game->gameId}.ini")){
                continue;
            }
            $configGame = new Zend_Config_Ini(CONFIG_PATH."/{$game->gameId}.ini", APPLICATION_ENV);
            
            /*
            if(isset($configGame->channel)){
                $stt = 1;
                foreach($configGame->channel as $key=>$value){
                    //add channel to DB
                    $dataChannel = array(
                                    'channelId'     => $key,
                                    'channelName'   => $value->name,
                                    'gameId'        => $game->gameId,
                                    'level'         => $stt
                                );
                    $modelGameChannel->addData($dataChannel);
                    //add channel server to DB
                    if(isset($value->value)){
                        foreach($value->value as $server){
                            $dataChannelServer = array(
                                    'channelId'     => $key,
                                    'serverId'      => $server,
                                    'gameId'        => $game->gameId
                                );
                            $modelGameChannelServer->addData($dataChannelServer);
                        }
                    }
                    $stt++;
                }
            }
            
            if(isset($configGame->server)){
                $stt = 1;
                foreach($configGame->server as $key=>$value){
                    //add server to DB
                    $dataServer = array(
                                    'serverId'      => $key,
                                    'serverName'    => $value->name,
                                    'gameId'        => $game->gameId,
                                    'level'         => $stt
                                );
                    $modelGameServer->addData($dataServer);
                    $stt++;
                }
            }
            
            if(isset($configGame->clusters)){
                $stt = 1;
                foreach($configGame->clusters as $key=>$value){
                    $dataCluster = array(
                                    'clusterId'     => $key,
                                    'clusterName'   => $value->name,
                                    'serverList'    => json_encode($value->value->toArray()),
                                    'type'          => 1,
                                    'gameId'        => $game->gameId,
                                    'level'         => $stt
                                );
                    $modelGameCluster->addData($dataCluster);
                    $stt++;
                }
            }
            if(isset($configGame->ccu->clusters)){
                $stt = 1;
                foreach($configGame->ccu->clusters as $key=>$value){
                    $dataCluster = array(
                                    'clusterId'     => $key,
                                    'clusterName'   => $value->name,
                                    'serverList'    => json_encode($value->value->toArray()),
                                    'type'          => 2,
                                    'gameId'        => $game->gameId,
                                    'level'         => $stt
                                );
                    $modelGameCluster->addData($dataCluster);
                    $stt++;
                }
            }
            */
        }
        exit;
    }
    public function indexAction(){
        $modelRole = new Admin_Model_Role();
        $this->view->modelRole = $modelRole;
        $roleUser = $modelRole->getDataByUserId($this->_auth['userId'])->toArray();
        $gameList = array();
        foreach($roleUser as $row){
            if(is_array($gameList) && is_array(json_decode($row['gameId']))){
                $gameList = array_merge($gameList, json_decode($row['gameId']));
            }
            if($row['homepage']){
                $homepage = $row['homepage'];
            }
        }
        if($gameList){
            $gameList = array_unique($gameList);
            if(count($gameList) == 1){
                if(!isset($homepage)){
                    $homepage = '/default/kpi/index';
                }
                $this->_redirect($homepage.'/game/'.$gameList[0]);
                return;
            }
            $gameList = '("'.implode('","',$gameList).'")';
                    
            $modelGame = new Default_Model_Game();
            $gameList = $modelGame->getDataByGameList($gameList)->toArray();
            foreach($gameList as $key=>$value){
                if(isset($value['gameId'])){
                    foreach($roleUser as $row){
                        $roleGameList = json_decode($row['gameId']);
                        if(is_array($roleGameList)){
                            if(in_array($value['gameId'], $roleGameList)){
                                $gameList[$key]['homepage'] = $row['homepage'];
                                break;
                            }
                        }
                    }
                }
            }

            $this->view->gameList = $gameList;
        }
        //prTien($gameList);
        $modelCategories = new Default_Model_Categories();
        $allCateList = $modelCategories->getAllData()->toArray();
        # Dem so luong game cua category
        foreach($allCateList as $key=>$row){
            $allCateList[$key]['total'] = 0;
            foreach($gameList as $row2){
                if($row2['catId'] == $row['catId'] || $row2['parentId'] == $row['catId']){
                    ++$allCateList[$key]['total'];
                }
            }
        }
        foreach($allCateList as $key=>$row){
            if($row['total'] <= 0){
                unset($allCateList[$key]);
            }
        }        
        foreach($allCateList as $key=>$row){
            if($row['parentId'] == 0 && $row['total'] > 0){
                #Tao cookie parent-menu
                if(!isset($_COOKIE['parent-menu']) && !isset($cookie_parent_menu)){
                    $cookie_parent_menu = $row['catId'];
                    setcookie('parent-menu',$cookie_parent_menu,time()+3600,'/');
                    $this->view->cookie_parent_menu = $cookie_parent_menu;
                }elseif(isset($_COOKIE['parent-menu'])){
                    $this->view->cookie_parent_menu = $_COOKIE['parent-menu'];
                }
                $allCateList[$key]['childList'] = $modelCategories->getDataByParentId($row['catId'])->toArray();
                if($allCateList[$key]['childList']){
                    foreach($gameList as $row2){
                        foreach($allCateList[$key]['childList'] as $row3){
                            if($row2['catId'] == $row3['catId']){
                                #Tao cookie child-menu
                                if(!isset($_COOKIE['child-menu']) && !isset($cookie_child_menu)){
                                    $cookie_child_menu = $row3['catId'];
                                    setcookie('child-menu',$cookie_child_menu,time()+3600,'/');
                                    $this->view->cookie_child_menu = $cookie_child_menu;
                                }elseif(isset($_COOKIE['child-menu'])){
                                    $this->view->cookie_child_menu = $_COOKIE['child-menu'];
                                }
                                break;
                            }
                        }
                        if(isset($cookie_child_menu)){
                            break;
                        }
                    }
                }
            }
        }
        $this->view->allCateList = $allCateList;
        
    }
}